package com.it.smartoldb.controller;

import org.springframework.data.domain.Page;
import com.it.smartoldb.common.Result;
import com.it.smartoldb.common.ResultCode;
import com.it.smartoldb.entity.Elderly;
import com.it.smartoldb.service.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 老人管理控制器
 */
@RestController
@RequestMapping("/api/elderly")
public class ElderlyController {

    @Autowired
    private ElderlyService elderlyService;

    /**
     * 创建老人信息
     */
    @PostMapping
    public Result<Elderly> createElderly(@Valid @RequestBody Elderly elderly) {
        try {
            Elderly created = elderlyService.createElderly(elderly);
            return Result.success(created);
        } catch (RuntimeException e) {
            return Result.error(ResultCode.PARAM_ERROR, e.getMessage());
        }
    }

    /**
     * 更新老人信息
     */
    @PutMapping("/{id}")
    public Result<Elderly> updateElderly(@PathVariable Long id, 
                                        @Valid @RequestBody Elderly elderly) {
        try {
            Elderly updated = elderlyService.updateElderly(id, elderly);
            return Result.success(updated);
        } catch (RuntimeException e) {
            return Result.error(ResultCode.PARAM_ERROR, e.getMessage());
        }
    }

    /**
     * 删除老人信息
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteElderly(@PathVariable Long id) {
        try {
            elderlyService.deleteElderly(id);
            return Result.success(null);
        } catch (RuntimeException e) {
            return Result.error(ResultCode.PARAM_ERROR, e.getMessage());
        }
    }

    /**
     * 根据ID查找老人信息
     */
    @GetMapping("/{id}")
    public Result<Elderly> getElderlyById(@PathVariable Long id) {
        Elderly elderly = elderlyService.findById(id);
        if (elderly != null) {
            return Result.success(elderly);
        } else {
            return Result.error(ResultCode.NOT_FOUND, "老人信息不存在");
        }
    }

    /**
     * 分页查询老人信息
     */
    @GetMapping
    public Result<Page<Elderly>> getElderlyList(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        
        Page<Elderly> elderlyPage = elderlyService.findAll(current, size);
        return Result.success(elderlyPage);
    }

    /**
     * 复合条件搜索老人信息
     */
    @GetMapping("/search")
    public Result<Page<Elderly>> searchElderly(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String idCard,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        
        Page<Elderly> elderlyPage = elderlyService.findByConditions(name, idCard, status, current, size);
        return Result.success(elderlyPage);
    }

    /**
     * 根据状态查询老人
     */
    @GetMapping("/status/{status}")
    public Result<Page<Elderly>> getElderlyByStatus(@PathVariable String status,
                                                     @RequestParam(defaultValue = "1") int current,
                                                     @RequestParam(defaultValue = "10") int size) {
        Page<Elderly> elderlyPage = elderlyService.findByStatus(status, current, size);
        return Result.success(elderlyPage);
    }

    /**
     * 获取统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        Map<String, Object> statistics = Map.of(
                "totalCount", elderlyService.getTotalCount()
        );
        return Result.success(statistics);
    }

    /**
     * 获取需要重点关注的老人
     */
    @GetMapping("/high-risk")
    public Result<List<Elderly>> getHighRiskElderly() {
        List<Elderly> highRiskElderly = elderlyService.getHighRiskElderly();
        return Result.success(highRiskElderly);
    }

    /**
     * 根据身份证号查找老人
     */
    @GetMapping("/id-card/{idCard}")
    public Result<Elderly> getElderlyByIdCard(@PathVariable String idCard) {
        Elderly elderly = elderlyService.findByIdCard(idCard);
        if (elderly != null) {
            return Result.success(elderly);
        } else {
            return Result.error(ResultCode.NOT_FOUND, "未找到对应身份证号的老人信息");
        }
    }

    /**
     * 根据用户ID查找老人信息
     */
    @GetMapping("/user/{userId}")
    public Result<Elderly> getElderlyByUserId(@PathVariable Long userId) {
        Elderly elderly = elderlyService.findByUserId(userId);
        if (elderly != null) {
            return Result.success(elderly);
        } else {
            return Result.error(ResultCode.NOT_FOUND, "未找到对应用户的老人信息");
        }
    }
} 